//
//  NSMutableAttributedString+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSMutableAttributedString+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation NSMutableAttributedString (THHook)

+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cla  = NSClassFromString(@"NSConcreteMutableAttributedString");
        SEL selectors[] = {
            @selector(initWithString:),
            @selector(initWithString:attributes:),

        };
        SEL hookselectors[] = {
            @selector(avoidCrashInitWithString:),
            @selector(avoidCrashInitWithString:attributes:),
        };
        for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
            SEL originalSelector = selectors[index];
            SEL swizzledSelector = hookselectors[index];
            BOOL hook = ExchangeImplementationsInTwoClasses(cla,originalSelector,[NSMutableAttributedString class], swizzledSelector);
            if (!hook) {
                NSLog(@"拦截失败");
            }
        }
    });
}
- (instancetype)avoidCrashInitWithString:(NSString *)str {
    id object = nil;
    @try {
        object = [self avoidCrashInitWithString:str];
    }@catch (NSException *exception) {
        [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        return object;
    }
}
#pragma mark - initWithString:attributes:
- (instancetype)avoidCrashInitWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs {
    id object = nil;
    @try {
        object = [self avoidCrashInitWithString:str attributes:attrs];
    }@catch (NSException *exception) {
      [[THCrashManager shareInstance] reportExp:exception];
    }@finally {
        return object;
    }
}


@end
